from ofxparse import OfxParser

class Transactions():
    '''
    A new TransactionList class that adds methods to use the OfxParser library
    '''
    
    transaction_list = []

    def __init__(self, filename = ""):
        '''
        Constructor: Creates a list of Transactions from an file passed as a parameter
        '''
        if filename:
            self.transaction_list = OfxParser.parse(file(filename)).account.statement.transactions
            
    #REQUIRES: optional filter parameters
    #MODIFIES: none (creates a new transaction list, does not modify original object)
    #EFFECTS:  the output transactions comes filtered to the parameter specifications
    
    '''
    payee = who you are making your transaction with
    type = what type of transaction (pos stands for purchase of sale, ofx uses this notation)
    date = datetime format
    amount = as a string object, need to parse to float
    id = the transaction id number
    memo = "IDP Purchase" etc
    
    '''
    def filter(self, payee = "", type = "", date = "", amount = "", id = "", memo =""):
        
        unfiltered_transactions = self.transaction_list
        filtered_transactions = Transactions()
        
        for element in unfiltered_transactions:
            if payee == element.payee or payee == "":    #adding if i set one parameters to none
                filtered_transactions.transaction_list.append(element)
                
        for element in unfiltered_transactions:
            if (type != element.type and type != ""):
                filtered_transactions.transaction_list.remove(element)
        
        for element in unfiltered_transactions:
            if (date != str(element.date) and date != ""):         #Date is in format "d-m-yyyy"
                filtered_transactions.transaction_list.remove(element)
        
        for element in unfiltered_transactions:
            if (amount != element.amount and amount != ""):
                filtered_transactions.transaction_list.remove(element)
                
        for element in unfiltered_transactions:
            if (id != element.id and id != ""):
                filtered_transactions.transaction_list.remove(element)
                
        for element in unfiltered_transactions:
            if (memo != element.memo and memo != ""):
                filtered_transactions.transaction_list.remove(element)                
            
        return filtered_transactions
    
    #REQUIRES: self
    #MODIFIES: none
    #EFFECTS: returns the total of the amounts
    def amountTotal(self):
        total = 0.0
        for element in self.transaction_list:
            total = float(element.amount) + total
        return total
            
    
    def printTransactions(self):
        for element in self.transaction_list:
            print "Name: " + element.payee + " " + "Type: " + element.type + " "  + "Amount: " + element.amount + " " + "Id: " + element.id + " " + "Memo: " + element.memo
            
            
            

        